﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SCRUM.Core.BusinessObjects;
using SCRUM.Core.DataProviders;
using SCRUM.Core.MembershipProviders;
using System.Web.Security;

namespace SCRUM.Core.DAO
{
    /// <summary>
    /// Data access object user
    /// </summary>
    public static class DAOUser
    {
        /// <summary>
        /// Saves the specified user.
        /// </summary>
        /// <param name="user">The specific user.</param>
        public static User Create(User user)
        {
            return MembershipProviderFactory.MembershipProvider.CreateUser(user);// ProviderFactory.Provider.InsertUser(user);
        }

        /// <summary>
        /// Updates the specified user.
        /// </summary>
        /// <param name="user">The specific user.</param>
        public static void Update(User user)
        {
            MembershipProviderFactory.MembershipProvider.UpdateUser(user);
        }

        /// <summary>
        /// Deletes the specified user.
        /// </summary>
        /// <param name="user">The specific user.</param>
        public static void Delete(User user)
        {
            ProviderFactory.Provider.DeleteUser(user);
        }

        public static void DisableUser(string login)
        {
            MembershipProviderFactory.MembershipProvider.LockUser(login);
        }

        /// <summary>
        /// Gets the users by all specified parameters. if any of parameters is -1 or null it will not be used in search
        /// </summary>
        /// <param name="id">The id.</param>
        /// <param name="login">The login.</param>
        /// <param name="fullname">The fullname.</param>
        /// <param name="email">The email.</param>
        /// <param name="enabled">if set to <c>true</c> [enabled].</param>
        /// <param name="telephone">The telephone.</param>
        /// <param name="im">The im.</param>
        /// <param name="defaultRoleID">The default role ID.</param>
        /// <returns>collection of Users</returns>
        public static IList<User> GetUsers(int id, string login, string fullname, string email, bool? enabled, string telephone, string im, int defaultRoleID)
        {
            return ProviderFactory.Provider.GetUsers(id, login, fullname, email, enabled, telephone, im, defaultRoleID);
        }

        /// <summary>
        /// Gets user by login.
        /// </summary>
        /// <param name="login">The login.</param>
        /// <returns>The user with specific login</returns>
        public static User GetByLogin(string login)
        {
            return ProviderFactory.Provider.GetUserByLogin(login);
        }

        /// <summary>
        /// Gets the user by ID.
        /// </summary>
        /// <param name="userID">The user ID.</param>
        /// <returns></returns>
        public static User GetUserByID(int userID)
        {
            return MembershipProviderFactory.MembershipProvider.GetUserByID(userID);
        }

        /// <summary>
        /// Gets all users.
        /// </summary>
        /// <returns></returns>
        public static IList<User> GetTeamMembers()
        {
            return GetTeamMembers(null, null);
        }

        /// <summary>
        /// Gets users in specified team.
        /// </summary>
        /// <param name="team">The team.</param>
        /// <returns></returns>
        public static IList<User> GetTeamMembers(Team team)
        {
            return GetTeamMembers(team, null);
        }

        /// <summary>
        /// Gets users associated to the task.
        /// </summary>
        /// <param name="task">The task.</param>
        /// <returns>List of tasks.</returns>
        public static IList<User> GetUsers(Task task)
        {
            return ProviderFactory.Provider.GetTeamMembers(task);
        }

        /// <summary>
        /// Gets users in specified team and role.
        /// </summary>
        /// <param name="team">The team.</param>
        /// <param name="role">The role.</param>
        /// <returns></returns>
        public static IList<User> GetTeamMembers(Team team, Role role)
        {
            return ProviderFactory.Provider.GetTeamMembers(team, role);
        }

        /// <summary>
        /// Gets the users which can be assigned to the task.
        /// </summary>
        /// <param name="team">The team.</param>
        /// <param name="task">The task.</param>
        /// <returns></returns>
        public static IList<User> GetUsersExceptTaskUsers(Team team, Task task)
        {
            return ProviderFactory.Provider.GetUsersExceptTaskUsers(team, task);
        }

        /// <summary>
        /// Gets the users which can be assigned to the task.
        /// </summary>
        /// <param name="taskID">The task ID.</param>
        /// <returns></returns>
        public static IList<User> GetUsersExceptTaskUsers(int taskID)
        {
            return ProviderFactory.Provider.GetUsersExceptTaskUsers(taskID);
        }

        /// <summary>
        /// Gets the logged user.
        /// </summary>
        /// <returns></returns>
        public static User GetLoggedUser()
        {
            return MembershipProviderFactory.MembershipProvider.GetUser();
        }

        /// <summary>
        /// Changes the full name uf user
        /// </summary>
        /// <param name="login">The login.</param>
        /// <param name="fullname">The fullname.</param>
        /// <returns></returns>
        public static bool ChangeFullName(string login, string fullname)
        {
            return ProviderFactory.Provider.ChangeFullName(login, fullname);
        }

        /// <summary>
        /// Changes the password.
        /// </summary>
        /// <param name="login">The login.</param>
        /// <param name="oldPassword">The old password.</param>
        /// <param name="newPassword">The new password.</param>
        /// <param name="status">The status.</param>
        /// <returns></returns>
        public static bool ChangePassword(string login, string oldPassword, string newPassword, out string status)
        {
            return MembershipProviderFactory.MembershipProvider.ChangePassword(login, oldPassword, newPassword, out status);
        }

        /// <summary>
        /// Resets the password.
        /// </summary>
        /// <param name="login">The login.</param>
        /// <returns></returns>
        public static bool ResetPassword(int userID, string password)
        {
            return MembershipProviderFactory.MembershipProvider.ResetPasswordByAdmin(userID, password);
        }

        /// <summary>
        /// Gets the user by ID.
        /// </summary>
        /// <param name="userID">The user ID.</param>
        /// <returns></returns>
        public static User GetByID(int userID)
        {
            return MembershipProviderFactory.MembershipProvider.GetUserByID(userID);
        }

		/// <summary>
		/// Converts story points to hours.
		/// </summary>
		/// <param name="storyPoints">The story points.</param>
		/// <returns>Hours.</returns>
		public static double StoryPointsToHours(double storyPoints)
		{
			return storyPoints * GetLoggedUser().Velocity;
		}

		/// <summary>
		/// Converts hours to story points.
		/// </summary>
		/// <param name="hours">The hours.</param>
		/// <returns></returns>
		public static double HoursToStoryPoints(double hours)
		{
			return hours / GetLoggedUser().Velocity;
		}

		/// <summary>
		/// Converts hours to story points.
		/// </summary>
		/// <param name="hours">The hours.</param>
		/// <param name="user">The user.</param>
		/// <returns></returns>
		public static double HoursToStoryPoints(double hours, User user)
		{
			return hours / user.Velocity;
		}

        /// <summary>
        /// Gets the user profile ID.
        /// </summary>
        /// <param name="defaultRoleID">ID of profile User.</param>
        /// <returns></returns>
        public static int GetUserProfileID(int defaultRoleID)
        {
            return MembershipProviderFactory.MembershipProvider.GetUserProfileID(defaultRoleID);
        }
	}
}
